﻿// Spun AJAX Framework
// Copyright (C) 2008 Blake Hegerle
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Formality.FormalSystem
{
    public class Definition : Sentence
    {
        public static char DEFINED = '\u2254';

        public Definition(Proposition Definiendum, Proposition Definiens)
        {
            this.Definiendum = Definiendum;
            this.Definiens = Definiens;
        }

        public Proposition Definiendum { get; private set; }
        public Proposition Definiens { get; private set; }

        #region Sentence overrides
        public override Inference? Implies(Proposition Goal, IEnumerable<Sentence> Given)
        {
            var GoalProp = Goal as Proposition;

            if (GoalProp == null)
                return null;

            var Inference = FindDefiniendum(GoalProp, Given, Definiendum, Definiens);

            if (Inference.HasValue)
                return Inference;

            return FindDefiniendum(GoalProp, Given, Definiens, Definiendum);
        }

        public override bool Equals(Sentence obj)
        {
            var that = obj as Definition;

            if (that == null)
                return false;

            return Definiendum.Equals(that.Definiendum)
                && Definiens.Equals(that.Definiens);
        }

        public override int HashCode
        {
            get { return Definiendum.GetHashCode() ^ Definiens.GetHashCode(); }
        }

        public override void Serialize(TextWriter Writer, bool Detailed)
        {
            Definiendum.Serialize(Writer, Detailed);
            Writer.Write(" {0} ", DEFINED);
            Definiens.Serialize(Writer, Detailed);
        }

        public override void Bind(Bindings Bindings, int Depth)
        {
            Definiendum.Bind(Bindings, Depth);
            Definiens.Bind(Bindings, Depth);
        }
        #endregion

        private Inference? FindDefiniendum(Proposition Goal, IEnumerable<Sentence> Given, Proposition Definiendum, Proposition Definiens)
        {
            foreach (var Potentials in Goal.ApplyTemplate(Definiendum, Definiens))
            {
                var Inferer = Given.FirstOrDefault(p => p.Equals(Potentials));

                if (Inferer != null)
                    return new Inference
                    {
                        Antecedents = new Sentence[] { this, Inferer },
                        Description = string.Format("{0} by {1}", Inferer.DisplayId, DisplayId)
                    };
            }

            return null;
        }
    }
}
